package cn.prophet.service.impl;

import cn.prophet.bean.BjksRecord;
import cn.prophet.bean.BjksRecordExample;
import cn.prophet.bean.GameOrder;
import cn.prophet.bean.User;
import cn.prophet.mapper.BjksRecordMapper;
import cn.prophet.mapper.GameOrderMapper;
import cn.prophet.mapper.UserMapper;
import cn.prophet.service.OrderService;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private GameOrderMapper orderMapper;
    @Resource
    BjksRecordMapper recordMapper;
    @Resource
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, String> unDoOrder(String orderIds) {
        log.info("orderIds:" + orderIds);
        Map<String, String> map = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        String failOrder = null;
        List<Long> list = JSON.parseArray(orderIds, Long.class);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Long orderId = list.get(i);
                GameOrder gameOrder = orderMapper.selectByPrimaryKey(orderId);
                if (gameOrder.getDealStatus() != 0) {
                    sb.append(gameOrder.getRecordId() + "已撤销,");
                    continue;
                }
                String recordId = gameOrder.getRecordId();
                BjksRecord currentRecord = recordMapper.selectCurrentRecord(gameOrder.getGameCategory());

                if (currentRecord != null) {
                    String num = currentRecord.getNum(); //最新的一期
                    if (Integer.parseInt(recordId) > Integer.parseInt(num) && gameOrder.getDealStatus() == 0) {
                        //下单期号大于最新期号可以退单
                        GameOrder order = new GameOrder();
                        order.setId(orderId);
                        order.setDealStatus((byte) 3);
                        orderMapper.updateByPrimaryKeySelective(order);
                        User user = new User();
                        user.setUserId(gameOrder.getUserId());
                        user.setBalance(gameOrder.getBettingAmount());
                        userMapper.addBalanceByPrimaryKey(user);

                    } else if (Integer.parseInt(recordId) == Integer.parseInt(num) && gameOrder.getDealStatus() == 0) {
                        Date endDate = currentRecord.getEndTime();

                        if (endDate.getTime() > System.currentTimeMillis()) {
                            //可以撤单
                            GameOrder order = new GameOrder();
                            order.setId(orderId);
                            order.setDealStatus((byte) 3);
                            orderMapper.updateByPrimaryKeySelective(order);
                            User user = new User();
                            user.setUserId(gameOrder.getUserId());
                            user.setBalance(gameOrder.getBettingAmount());
                            userMapper.addBalanceByPrimaryKey(user);

                        } else {
                            sb.append(gameOrder.getRecordId() + "撤销失败,");

                        }
                    } else {
                        sb.append(gameOrder.getRecordId() + "撤销失败,");
                    }
                } else {
                    sb.append(gameOrder.getRecordId() + "撤销失败,");

                }

            }
            if (sb.toString().length() > 0) {
                failOrder = sb.toString().substring(0, sb.toString().length() - 1);
            }

            map.put("code", "1");
            if (failOrder == null) {
                map.put("msg", "撤单成功");
            } else {
                map.put("msg",failOrder);
            }


        } else {
            map.put("code", "0");
            map.put("msg", "传入的参数格式化失败");
        }
        return map;
    }


}
